package com.js.rtu.mvp.present;


import android.util.Log;

import com.js.rtu.base.BasePresenter;
import com.js.rtu.config.EnableConfig;
import com.js.rtu.config.IniConfig;
import com.js.rtu.config.PowerTimeConfig;
import com.js.rtu.config.SamplingPeriodConfig;
import com.js.rtu.config.WorkingModeConfig;
import com.js.rtu.fragment.third.DebugReadBean;
import com.js.rtu.mvp.bean.Cmd92Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H17_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H18_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H19_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H20_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H21_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H24_Bean;
import com.js.rtu.mvp.bean.base.Cmd92_H25_Bean;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.util.BleUtils;

import org.greenrobot.eventbus.EventBus;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 92查询所有参数数据解析
 * Created by wz on 2019/5/17.
 */
public class Cmd92Presenter implements BasePresenter<Cmd92Bean> {

    int index;

    @Override
    public CommonBean<Cmd92Bean> paseData(String msg) {
        Log.e("xxxx", msg);
        CommonBean<Cmd92Bean> bean = new CommonBean<>();
        Cmd92Bean cmd92Bean = new Cmd92Bean();
        index = 0;
        String[] data = msg.split(" ");
        if (getDataByIndex(data, 0).equals("EE") && getDataByIndex(data, 1).equals("EE")
                && getDataByIndex(data, data.length - 1).equals("FF")) {

            //帧头、CRC、帧尾
            bean.setStart(getDataByIndex(data, 0) + getDataByIndex(data, 1));
            bean.setCRC(getDataByIndex(data, data.length - 4) + getDataByIndex(data, data.length - 3));
            bean.setEnd(getDataByIndex(data, data.length - 1) + getDataByIndex(data, data.length - 2));
            index = 2;//2字节

            //address
            StringBuilder str_address = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                str_address.append(getDataByIndex(data, index + i));
            }
            index += 8;//8字节
            String address = BleUtils.hexStr2Str(str_address.toString());
            bean.setAddress(address);

            //length固定长度2，hex
            int length = BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index));
            index += 2;//2字节
            bean.setLength(length);

            //cmd固定长度1，hex
            int cmd = Integer.parseInt(getDataByIndex(data, index));
            index += 1;//1字节
            bean.setCmd(cmd);
            boolean isBreak = false;
            //这里开始就是data数据
            while (index < data.length - 1 && !isBreak) {
                //识别的字段
                String start = getDataByIndex(data, index++).toUpperCase();

                int currentData;
                switch (start) {
                    case "01":
                        //定时采集间隔,2字节,低位在前
                        currentData = BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index));
                        cmd92Bean.setStr_01H(String.valueOf(currentData));
                        index += 2;
                        break;
                    case "02":
                        //定时储间隔,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        cmd92Bean.setStr_02H(String.valueOf(currentData));
                        index += 1;
                        break;
                    case "03":
                        //定时自报间隔,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        cmd92Bean.setStr_03H(String.valueOf(currentData));
                        index += 1;
                        break;
                    case "04":
                        //基准电压,4字节
                        cmd92Bean.setStr_04H(BleUtils.byte2Float(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2)
                                + getDataByIndex(data, index + 1) + getDataByIndex(data, index)));
                        index += 4;
                        break;
                    case "05":
                        //工作模式,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        cmd92Bean.setStr_05H(WorkingModeConfig.getWorkingMode(currentData));
                        index += 1;
                        break;
                    case "06":
                        //终端报警间隔及次数,1字节
                        int interval = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        int count = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        cmd92Bean.setStr_06H(interval + "," + count);
                        break;
                    case "07":
                        //报警使能,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        cmd92Bean.setStr_07H(EnableConfig.getEnable(currentData));

                        break;
                    case "08":
                        //报警手机数,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;

                        List<String> str_08H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            //例如： 手机号码为 13812345678， 数据格式为 01 38 12 34 56 78
                            str_08H.add(getDataByIndex(data, index) + getDataByIndex(data, index + 1) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 3)
                                    + getDataByIndex(data, index + 4) + getDataByIndex(data, index + 5));
                            index += 6;
                        }
                        cmd92Bean.setStr_08H(str_08H);
                        break;
                    case "09":
                        //出厂编号,8字节,ASCII字符串
                        StringBuilder strNum = new StringBuilder();
                        for (int i = 0; i < 8; i++) {
                            strNum.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_09H(BleUtils.hexStr2Str(strNum.toString()));
                        index += 8;
                        break;
                    case "10":
                        //DTU 识别号,11字节,ASCII字符串
                        StringBuilder strDtu = new StringBuilder();
                        for (int i = 0; i < 11; i++) {
                            strDtu.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_10H(BleUtils.hexStr2Str(strDtu.toString()));
                        index += 11;
                        break;
                    case "11":
                        //中心站 IP 地址,15字节,ASCII字符串
                        StringBuilder strIP = new StringBuilder();
                        for (int i = 0; i < 15; i++) {
                            strIP.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_11H(BleUtils.hexStr2Str(strIP.toString()));
                        index += 15;
                        break;
                    case "12":
                        //端口,5字节,ASCII字符串
                        StringBuilder strPort = new StringBuilder();
                        for (int i = 0; i < 5; i++) {
                            strPort.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_12H(BleUtils.hexStr2Str(strPort.toString()));
                        index += 5;
                        break;
                    case "13":
                        //中心接入点 APN,25字节,ASCII字符串
                        StringBuilder strAPN = new StringBuilder();
                        for (int i = 0; i < 25; i++) {
                            strAPN.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_13H(BleUtils.hexStr2Str(strAPN.toString()));
                        index += 25;
                        break;
                    case "14":
                        //中心域名,65字节,ASCII字符串
                        StringBuilder strCenter = new StringBuilder();
                        for (int i = 0; i < 65; i++) {
                            strCenter.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_14H(BleUtils.hexStr2Str(strCenter.toString()));
                        index += 65;
                        break;
                    case "15":
                        //用户名,65字节,ASCII字符串
                        StringBuilder strUser = new StringBuilder();
                        for (int i = 0; i < 65; i++) {
                            strUser.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_15H(BleUtils.hexStr2Str(strUser.toString()));
                        index += 65;
                        break;
                    case "16":
                        //用户密码,33字节,ASCII字符串
                        StringBuilder strPwd = new StringBuilder();
                        for (int i = 0; i < 33; i++) {
                            strPwd.append(getDataByIndex(data, index + i));
                        }
                        cmd92Bean.setStr_16H(BleUtils.hexStr2Str(strPwd.toString()));
                        index += 33;
                        break;
                    case "17":
                        //RS485 通信口数,1字节
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        if (currentData == 0) {
                            List<Cmd92_H17_Bean> str_17H = new ArrayList<>();
                            cmd92Bean.setStr_17H(str_17H);
                            break;
                        }
                        // 表计总数
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        int len_17 = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        List<Cmd92_H17_Bean> str_17H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H17_Bean bean_17H = new Cmd92_H17_Bean();
                            bean_17H.setId(getDataByIndex(data, index));
                            bean_17H.setEnable(EnableConfig.getEnable(BleUtils.hToint(getDataByIndex(data, index + 1))));
                            //协议
                            int xieyiPos = BleUtils.hToint(getDataByIndex(data, index + 2));
                            xieyiPos--;
                            if (xieyiPos < IniConfig.agreement_data.size())
                                bean_17H.setAgreement(IniConfig.agreement_data.get(xieyiPos));

                            //波特率
                            bean_17H.setBaudRate(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 3)) + "",
                                    IniConfig.baudRate_des, IniConfig.baudRate_data));

                            //数据位
                            bean_17H.setDataBit(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 4)) + "",
                                    IniConfig.dataBit_des, IniConfig.dataBit_data));
                            //停止位
                            bean_17H.setStopBit(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 5)) + "",
                                    IniConfig.stopBit_des, IniConfig.stopBit_data));
                            //校验位
                            bean_17H.setCheckBit(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 6)) + "",
                                    IniConfig.checkBit_des, IniConfig.checkBit_data));


                            bean_17H.setAddress(getDataByIndex(data, index + 7) + getDataByIndex(data, index + 8) + "" + getDataByIndex(data, index + 9) + getDataByIndex(data, index + 10));
                            bean_17H.setPowerTime(PowerTimeConfig.getTime(BleUtils.hToint(getDataByIndex(data, index + 11))));
                            bean_17H.setPowerVoltage(BleUtils.hToint(getDataByIndex(data, index + 12)) + "V");
                            bean_17H.setSamplingPeriod(SamplingPeriodConfig.getTime
                                    (BleUtils.hToint(getDataByIndex(data, index + 14) + getDataByIndex(data, index + 13))));
                            str_17H.add(bean_17H);
                            index += len_17;
                        }
                        cmd92Bean.setStr_17H(str_17H);

                        break;
                    case "18":
                        //模拟量表计参数 通信口数,1字节
                        //表计总数 m
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        if (currentData == 0) {
                            List<Cmd92_H18_Bean> str_18H = new ArrayList<>();
                            cmd92Bean.setStr_18H(str_18H);
                            break;
                        }
                        //每组表计参数长度
                        int len_18 = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        int num = len_18 * currentData;//数量X长度
                        if (!getDataByIndex(data, num).equals("19")) {//下一个标识
                            //这里这么写是为了解决固件的错误
                            //1.64时这里len_18的长度应该是32，而1.63时是28
                            //但是结果1.64和1.63都是28，手动判断下，解决这个长度错误带来的问题
                            len_18 = 32;
                        }

                        List<Cmd92_H18_Bean> str_18H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H18_Bean bean_18H = new Cmd92_H18_Bean();
                            bean_18H.setId(getDataByIndex(data, index));
                            //表计类型
                            bean_18H.setType(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 1)) + "",
                                    IniConfig.moNiLiangBiao_des,
                                    IniConfig.moNiLiangBiao_data));


                            bean_18H.setOutputMin(BleUtils.byte2Float(getDataByIndex(data, index + 5) + getDataByIndex(data, index + 4)
                                    + getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2)));
                            bean_18H.setOutputMax(BleUtils.byte2Float(getDataByIndex(data, index + 9) + getDataByIndex(data, index + 8)
                                    + getDataByIndex(data, index + 7) + getDataByIndex(data, index + 6)));
                            bean_18H.setPowerVoltage(BleUtils.hToint(getDataByIndex(data, index + 10)) + "V");
                            bean_18H.setRangeMax(BleUtils.byte2Float(getDataByIndex(data, index + 14) + getDataByIndex(data, index + 13)
                                    + getDataByIndex(data, index + 12) + getDataByIndex(data, index + 11)));
                            bean_18H.setRangeMin(BleUtils.byte2Float(getDataByIndex(data, index + 18) + getDataByIndex(data, index + 17)
                                    + getDataByIndex(data, index + 16) + getDataByIndex(data, index + 15)));
                            bean_18H.setAlarmMin_reduce(BleUtils.hToint(getDataByIndex(data, index + 20) + getDataByIndex(data, index + 19)) + "");
                            bean_18H.setAlarmMin(BleUtils.hToint(getDataByIndex(data, index + 22) + getDataByIndex(data, index + 21)) + "");
                            bean_18H.setAlarmMax(BleUtils.hToint(getDataByIndex(data, index + 24) + getDataByIndex(data, index + 23)) + "");
                            bean_18H.setAlarmMax_plus(BleUtils.hToint(getDataByIndex(data, index + 26) + getDataByIndex(data, index + 25)) + "");

                            bean_18H.setEnable(EnableConfig.getEnable(BleUtils.hToint(getDataByIndex(data, index + 27))));

                            //1.64协议多了基准电压，4字节，1.63没有
                            if (len_18 >= 32) {
                                bean_18H.setReferenceVoltage(BleUtils.byte2Float(getDataByIndex(data, index + 31) + getDataByIndex(data, index + 30)
                                        + getDataByIndex(data, index + 29) + getDataByIndex(data, index + 28)));
                            } else {
                                bean_18H.setReferenceVoltage("无");
                            }
                            str_18H.add(bean_18H);


                            index += len_18;
                        }
                        cmd92Bean.setStr_18H(str_18H);

                        break;
                    case "19":
                        //开关量总数
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        List<Cmd92_H19_Bean> str_19H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H19_Bean bean_19H = new Cmd92_H19_Bean();
                            bean_19H.setId(getDataByIndex(data, index));
                            bean_19H.setState(getDataByIndex(data, index + 1));
                            bean_19H.setEnable(EnableConfig.getEnable(Integer.parseInt(getDataByIndex(data, index + 2))));


                            //报警类型
                            bean_19H.setAlarmType(IniConfig.getDes(
                                    Integer.parseInt(getDataByIndex(data, index + 3)) + "",
                                    IniConfig.alarm_des,
                                    IniConfig.alarm_data));


                            //                            bean_19H.setAlarmType(AlarmTypeConfig.getAlarm(Integer.parseInt(getDataByIndex(data,index+3))));
                            str_19H.add(bean_19H);
                            index += 4;
                        }
                        cmd92Bean.setStr_19H(str_19H);
                        break;
                    case "20":
                        //脉冲量总数
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        List<Cmd92_H20_Bean> str_20H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H20_Bean bean_20H = new Cmd92_H20_Bean();


                            BigInteger bigint = new BigInteger(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2)
                                    + getDataByIndex(data, index + 1) + getDataByIndex(data, index), 16);
                            int initialValue = bigint.intValue();//双字
                            bigint = new BigInteger(getDataByIndex(data, index + 7) + getDataByIndex(data, index + 6)
                                    + getDataByIndex(data, index + 5) + getDataByIndex(data, index + 4), 16);

                            int fullValue = bigint.intValue();//双字
                            bean_20H.setInitialValue(initialValue + "");
                            bean_20H.setFullValue(fullValue + "");


                            str_20H.add(bean_20H);
                            index += 8;
                        }
                        cmd92Bean.setStr_20H(str_20H);
                        break;
                    case "21":
                        //根据供电方式切换工作模式
                        Cmd92_H21_Bean bean_21H = new Cmd92_H21_Bean();
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        bean_21H.setState(EnableConfig.getEnable(currentData));
                        index += 1;
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        bean_21H.setTotal(currentData);
                        List<String> list = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            list.add(WorkingModeConfig.getWorkingMode(BleUtils.hToint(getDataByIndex(data, index))));
                            index += 1;
                        }
                        bean_21H.setWorkType(list);
                        cmd92Bean.setStr_21H(bean_21H);

                        break;
                    case "22":
                        //模拟量表供电时间
                        cmd92Bean.setStr_22H(BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index)) + "");
                        index += 2;
                        break;
                    case "23":
                        //当前系统时间
                        String str_time = "20" +
                                BleUtils.hToint(getDataByIndex(data, index)) + "/" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 1))) +
                                "/" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 2))) +
                                " " +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 3))) +
                                ":" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 4))) +
                                ":" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 5)));
                        cmd92Bean.setStr_23H(str_time);
                        index += 6;
                        break;
                    case "24":
                        //多中心参数
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;

                        List<Cmd92_H24_Bean> str_24H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H24_Bean bean_24H = new Cmd92_H24_Bean();
                            StringBuilder ip = new StringBuilder();
                            ip.append(BleUtils.hToint(getDataByIndex(data, index))).append(".")
                                    .append(BleUtils.hToint(getDataByIndex(data, index + 1))).append(".")
                                    .append(BleUtils.hToint(getDataByIndex(data, index + 2))).append(".")
                                    .append(BleUtils.hToint(getDataByIndex(data, index + 3)));
                            bean_24H.setIp(ip.toString());
                            bean_24H.setPort(BleUtils.hToint(getDataByIndex(data, index + 5) + getDataByIndex(data, index + 4)) + "");

                            //连接类型
                            bean_24H.setType(IniConfig.getDes(
                                    BleUtils.hToint(getDataByIndex(data, index + 6)) + "",
                                    IniConfig.connectType_des,
                                    IniConfig.connectType_data));


                            //                            bean_24H.setType(ConnectConfig.getConnect(BleUtils.hToint(getDataByIndex(data,index+6))));
                            bean_24H.setEnable(EnableConfig.getEnable(BleUtils.hToint(getDataByIndex(data, index + 7))));
                            index += 8;
                            str_24H.add(bean_24H);
                        }
                        cmd92Bean.setStr_24H(str_24H);
                        break;
                    case "25":
                        //多段自报
                        //长度
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        index += 1;
                        List<Cmd92_H25_Bean> str_25H = new ArrayList<>();
                        for (int i = 0; i < currentData; i++) {
                            Cmd92_H25_Bean bean_25H = new Cmd92_H25_Bean();
                            bean_25H.setStart(BleUtils.hToint(getDataByIndex(data, index)) + ":" + BleUtils.hToint(getDataByIndex(data, index + 1)));
                            bean_25H.setEnd(BleUtils.hToint(getDataByIndex(data, index + 2)) + ":" + BleUtils.hToint(getDataByIndex(data, index + 3)));
                            bean_25H.setInterval(BleUtils.hToint(getDataByIndex(data, index + 4)) + "");
                            str_25H.add(bean_25H);
                            index += 5;
                        }
                        cmd92Bean.setStr_25H(str_25H);
                        index += 2;
                        index = data.length - 1;
                        isBreak = true;
                        break;
                    case "FF":
                        //帧尾
                        isBreak = true;
                        break;
                    default:
                        //以上标志都没,标志移到最后，结束解析
                        index = data.length - 1;
                        isBreak = true;
                        break;
                }
            }

        }
        bean.setData(cmd92Bean);
        return bean;

    }

    private String getDataByIndex(String[] data, int index) {
        StringBuilder stringBuilder = new StringBuilder();
        if (index < data.length) {
            stringBuilder.append(data[index]).append(" ");
        } else {
            this.index = data.length - 1;
        }
        if (stringBuilder.length() > 0) {
            return stringBuilder.substring(0, stringBuilder.length() - 1);
        } else {
            return stringBuilder.toString();
        }
    }

    public void setInfo(CommonBean<Cmd92Bean> bean) {
        String line = "-----------------------------------------------------------\n";
        Cmd92Bean data = bean.getData();


        StringBuilder stringBuilder = new StringBuilder();

        if (data.getStr_23H() != null) {
            stringBuilder.append(String.format("查询时间:%s\n", data.getStr_23H()));
        }
        if (data.getStr_09H() != null) {
            stringBuilder.append(String.format("出厂编号:%s\n", data.getStr_09H()));
        }
        if (data.getStr_10H() != null) {
            stringBuilder.append(String.format("DTU识别号:%s\n", data.getStr_10H()));
        }
        stringBuilder.append(line);


        if (data.getStr_24H() != null) {
            for (int i = 0; i < data.getStr_24H().size(); i++) {
                Cmd92_H24_Bean cmd92_h24_bean = data.getStr_24H().get(i);
                if (cmd92_h24_bean != null) {
                    stringBuilder
                            .append(String.format("中心%s:", i + ""))
                            .append(String.format("IP:%s,", cmd92_h24_bean.getIp()))
                            .append(String.format("端口=%s,", cmd92_h24_bean.getPort()))
                            .append(String.format("协议=%s,", cmd92_h24_bean.getType()))
                            .append(String.format("%s\n", cmd92_h24_bean.getEnable()));
                    //                    .append(line);
                }
            }
        }

        stringBuilder.append(line);
        if (data.getStr_13H() != null) {
            stringBuilder.append(String.format("APN:%s\n", data.getStr_13H()));
        }
        if (data.getStr_14H() != null) {
            stringBuilder.append(String.format("中心域名:%s\n", data.getStr_14H()));
        }
        if (data.getStr_15H() != null) {
            stringBuilder.append(String.format("用户名:%s\n", data.getStr_15H()));
        }
        if (data.getStr_16H() != null) {
            stringBuilder.append(String.format("用户密码:%s\n", data.getStr_16H()));
        }


        stringBuilder.append(line);

//        时间值=[,61,63,65,67,71,83]
//        描述=[2小时,4小时,6小时,8小时,12小时,24小时]


        if (data.getStr_01H() != null) {
            stringBuilder.append(String.format("采集间隔(秒):%s,", data.getStr_01H()));
        }
        if (data.getStr_02H() != null) {
            stringBuilder.append(String.format("存储间隔:%s,", getInterval(data.getStr_02H())));
        }
        if (data.getStr_03H() != null) {
            stringBuilder.append(String.format("自报间隔:%s\n", getInterval(data.getStr_03H())));
        }
        if (data.getStr_04H() != null) {
            stringBuilder.append(String.format("基准电压:%s,", data.getStr_04H()));
        }
        if (data.getStr_05H() != null) {
            stringBuilder.append(String.format("工作模式:%s\n", data.getStr_05H()));
        }
        stringBuilder.append(line);


        if (data.getStr_06H() != null) {
            stringBuilder.append(String.format("报警间隔及次数:%s,", data.getStr_06H()));
        }
        if (data.getStr_07H() != null) {
            stringBuilder.append(String.format("使能:%s\n", data.getStr_07H()));
        }
        if (data.getStr_08H() != null) {
            stringBuilder.append(String.format("报警手机数:%s\n", data.getStr_08H().size()));
        }


        if (data.getStr_08H() != null) {
            for (int i = 0; i < data.getStr_08H().size(); i++) {
                String phone = data.getStr_08H().get(i);
                if (phone != null) {
                    stringBuilder.append(String.format("报警号码编号%s:%s\n", i + "", phone));
                }

            }
        }
        stringBuilder.append(line);


        Cmd92_H21_Bean h21_bean = data.getStr_21H();
        if (h21_bean != null) {
            stringBuilder.append(String.format("供电方式数量:%s\n", h21_bean.getTotal()))
                    .append(String.format("自动切换工作模式:%s\n", h21_bean.getState()));
            if (h21_bean.getWorkType() != null) {
                for (int i = 0; i < h21_bean.getWorkType().size(); i++) {
                    String workType = h21_bean.getWorkType().get(i);
                    //第一个是接市电时的工作模式，第二个是接电池
                    if (workType != null) {
                        if (i == 0) {
                            stringBuilder.append(String.format("接市电=%s,", workType));
                        } else if (i == 1) {
                            stringBuilder.append(String.format("接电池=%s\n", workType));
                        } else {
                            stringBuilder.append(String.format("工作模式%s:%s\n", i, workType));
                        }
                    }


                }
            }
        }


        stringBuilder.append(line);

        if (data.getStr_17H() != null) {
            stringBuilder.append(String.format("RS485通信口数:%s\n", data.getStr_17H().size()));

            if (data.getStr_17H() != null) {
                for (int i = 0; i < data.getStr_17H().size(); i++) {
                    Cmd92_H17_Bean h17_bean = data.getStr_17H().get(i);
                    if (h17_bean != null) {
                        //RS485 通信口数,1字节
                        int num = i + 1;
                        stringBuilder.append(String.format("通信口[%s]:\n", num + ""))
                                .append(String.format("使能:%s;", h17_bean.getEnable()))
                                .append(String.format("协议=%s;", h17_bean.getAgreement()))
                                .append(String.format("地址=%s\n", h17_bean.getAddress()))
                                .append(String.format("供电时间(s)=%s;", h17_bean.getPowerTime()))
                                .append(String.format("供电电压(V)=%s;", h17_bean.getPowerVoltage()))
                                .append(String.format("周期(s)=%s\n", h17_bean.getSamplingPeriod()))
                                .append(String.format("波特率=%s;", h17_bean.getBaudRate()))
                                .append(String.format("数据位=%s;", h17_bean.getDataBit()))
                                .append(String.format("停止位=%s;", h17_bean.getStopBit()))
                                .append(String.format("校验位=%s\n", h17_bean.getCheckBit()))
                                .append(line);
                    }


                }
            }
        }


        if (data.getStr_22H() != null) {
            stringBuilder.append(String.format("模拟量表供电时间(毫秒):%s\n", data.getStr_22H()));
        }

        if (data.getStr_18H() != null) {
            stringBuilder.append(String.format("模拟量总数:%s\n", data.getStr_18H().size()));
        }

        if (data.getStr_18H() != null) {
            for (int i = 0; i < data.getStr_18H().size(); i++) {
                Cmd92_H18_Bean h18_bean = data.getStr_18H().get(i);
                if (h18_bean != null) {
                    int num = i + 1;
                    stringBuilder.append(String.format("[%s]", num + ""))
                            .append(String.format("%s,", h18_bean.getType()))
                            .append(String.format("供电电压:%s,", h18_bean.getPowerVoltage()))
                            .append(String.format("%s,", h18_bean.getOutputMin() + "--" + h18_bean.getOutputMax()))
                            .append(String.format("基准电压:%s\n", h18_bean.getReferenceVoltage()))
                            .append(String.format("使能标志:%s,", h18_bean.getEnable()))
                            .append(String.format("量程:%s\n", h18_bean.getRangeMin() + "--" + h18_bean.getRangeMax()));

                    //公式：pressure[chn-1] = ((v_ai_temp - 800) / 3200) * (volMax - volMin);
                    //v_ai_temp当前要转换的数值，800固定，3200固定，volMax量程最大值，volMin量程最小值
                    float max = Float.parseFloat(h18_bean.getRangeMax());
                    float min = Float.parseFloat(h18_bean.getRangeMin());
                    float fanwei = max - min;
                    stringBuilder
                            .append(String.format(Locale.CHINA,
                                    "下下限%s:[%.0fKpa]\n", h18_bean.getAlarmMin_reduce(),
                                    (((Integer.parseInt(h18_bean.getAlarmMin_reduce()) - 800) * fanwei) / 3200)))
                            .append(String.format(Locale.CHINA,
                                    "下限:%s:[%.0fKpa]\n", h18_bean.getAlarmMin(),
                                    (((Integer.parseInt(h18_bean.getAlarmMin()) - 800) * fanwei) / 3200)))
                            .append(String.format(Locale.CHINA,
                                    "上限:%s:[%.0fKpa]\n", h18_bean.getAlarmMax(),
                                    (((Integer.parseInt(h18_bean.getAlarmMax()) - 800) * fanwei) / 3200)))
                            .append(String.format(Locale.CHINA,
                                    "上上限:%s:[%.0fKpa]\n", h18_bean.getAlarmMax_plus(),
                                    (((Integer.parseInt(h18_bean.getAlarmMax_plus()) - 800) * fanwei) / 3200)))
                            .append(line);
                }


            }
        }
        if (data.getStr_19H() != null) {
            stringBuilder.append(String.format("开关量总数:%s\n", data.getStr_19H().size()))
                    .append("开关量逻辑状态:\n");
            for (int i = 0; i < data.getStr_19H().size(); i++) {

                Cmd92_H19_Bean h19_bean = data.getStr_19H().get(i);
                if (h19_bean != null) {
                    stringBuilder
                            .append(String.format("编号[%s],", h19_bean.getId()))
                            .append(String.format("逻辑:%s,", h19_bean.getState()))
                            .append(String.format("类型:%s,", h19_bean.getAlarmType()))
                            .append(String.format("使能:%s\n", h19_bean.getEnable()));

                }

            }
        }
        stringBuilder.append(line);
        if (data.getStr_20H() != null) {
            stringBuilder.append(String.format("脉冲量总数:%s\n", data.getStr_20H().size()));
            for (int i = 0; i < data.getStr_20H().size(); i++) {
                int num = i + 1;
                Cmd92_H20_Bean h20_bean = data.getStr_20H().get(i);
                if (h20_bean != null) {
                    stringBuilder.append(String.format("[%s]:", num + ""))
                            .append(String.format("初始值:%s,", h20_bean.getInitialValue()))
                            .append(String.format("满量程值:%s\n", h20_bean.getFullValue()));
                }

            }
            stringBuilder.append(line);
        }
        if (data.getStr_25H() != null) {
            stringBuilder.append(String.format("多段自报间隔总数:%s\n", data.getStr_25H().size()));
            for (int i = 0; i < data.getStr_25H().size(); i++) {
                int num = i + 1;
                Cmd92_H25_Bean h25_bean = data.getStr_25H().get(i);
                if (h25_bean != null) {
                    stringBuilder.append(String.format("(%s)时间段:", i + ""))
                            .append(String.format("%s", h25_bean.getStart()))
                            .append(String.format("至:%s,", h25_bean.getEnd()))
                            .append(String.format("间隔:%s\n", h25_bean.getInterval()));
                }


            }
            stringBuilder.append(line);
        }
        EventBus.getDefault().post(new DebugReadBean(stringBuilder.toString()));
    }
    public String getInterval(String data) {
        String interval = "";
        if (data != null) {
            if (data.equals("61")) {
                interval = "2小时";
            } else if (data.equals("63")) {
                interval = "4小时";
            } else if (data.equals("65")) {
                interval = "6小时";
            } else if (data.equals("67")) {
                interval = "8小时";
            } else if (data.equals("71")) {
                interval = "12小时";
            } else if (data.equals("83")){
                interval = "24小时";
            } else{
                interval = data + "分钟";
            }

        }
        return interval;
    }

}





